package com.example.rumen;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.Objects;

public class Sign_up extends AppCompatActivity {


    private EditText username_txt;
    private EditText password_txt;
    private EditText passwordagin_txt;
    private Button btn_signup;
    private String username_sign;
    private String password_sign;
    private String passagain;
    private Button btn_clear;

    @SuppressLint({"MissingInflatedId", "WrongViewCast"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_sign_up);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        username_txt = findViewById(R.id.editText_user_sign);
        password_txt = findViewById(R.id.editText_pass_sign);
        passwordagin_txt = findViewById(R.id.editText_passapagin);
        btn_signup = findViewById(R.id.button_signup);
        //注册按钮事件
        btn_clear = findViewById(R.id.button_clear);
        btn_signup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username_sign = username_txt.getText().toString();
                password_sign = password_txt.getText().toString();
                passagain = passwordagin_txt.getText().toString();

                if (!Objects.equals(password_sign, passagain)) {
                    Toast.makeText(getApplicationContext(), "两次密码不一致，请重新输入", Toast.LENGTH_LONG).show();
                    password_txt.setText("");
                    passwordagin_txt.setText("");
                    password_txt.requestFocus();

                } else {
                    Intent intent = new Intent(getApplicationContext(), Sign_in.class);
                    intent.putExtra("user",username_sign);
                    intent.putExtra("pass",password_sign);
                    Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_LONG).show();
                    startActivity(intent);
                    finish();
                }
            }
        });
        btn_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username_txt.setText("");
                password_txt.setText("");
                passwordagin_txt.setText("");

                username_txt.findFocus();
                password_txt.findFocus();
                passwordagin_txt.findFocus();
            }
        });


    }
}